$(document).ready(function(){
	initNote();
	noteValidate();
});
//提问ajax
function saveNote(status){
	if(isNoteValidate.checkForm()){
		var html = html_encode($("#editorhtml").val());
		var md = html_encode($("#editormd").val());
		var url = "/HyperIT/note/saveNote";
		var parm = "userDetail_id="+thisUser.id+"&status="+status+"&title="+html_encode($("#title").val())+"&content_html="+html+"&content_md="+md;
		$.post(url,parm,function(data){
			if (data.serviceResult) {
				alert(data.resultInfo);
				history.go(-1);
			} else{
				if(data.resultInfo){
					messageTip(data.resultInfo);
				}else{
					$("#sensitive").text("文中出现了敏感词！       标题："+data.title+"     内容："+data.content_md+"      请修改后发布，您可以使用工具栏的搜索功能对文章内容中的敏感词进行搜索。");
				}
			}
		});
	}else{
		isNoteValidate.showErrors();
	}
}
function exitNote(){
	var con = confirm("确定退出？");
	if(con){
		history.go(-1);
	}else{
		return;
	}
}
//文章表单验证
function noteValidate(){
	isNoteValidate = $("#articleForm").validate({
		onsubmit: false,
		onkeyup: false, 
		onfocusout: function(element){
			$(element).valid();
	  	},
   		errorPlacement: function(error, element) {  
   			error.appendTo(element.prev());
   		},
		rules:{
			title:{
				required:true,
				maxlength:20
			}
		},
		messages:{
			title:{
				required:"标题不能为空！",
				maxlength:"标题长度不可超过20！"
			}
		}
	});
}
//初始化页面
function initNote(){
	if(window.location.href.indexOf("?")!=-1){
		var url = "/HyperIT/note/findOneNote";
		var parm = "note_id="+window.location.href.split("?")[1]+"&userdetail_id="+thisUser.id;
		$.get(url,parm,function(data){
			if(data.serviceResult){
				var note = data.resultParm.note;
				noteId = note.id;
				
				
				$("#title").val(html_decode(note.title));
				$("#editormd").val(html_decode(note.content_md));
				$(".action a").eq(0).attr("onclick","updateNote(0)");
				$(".action a").eq(1).attr("onclick","updateNote(1)");
				$(".action a").eq(2).attr("onclick","exitNote()");
			}else{
				messageTip(data.resultInfo);
			}
		});
	}else{
		return;
	}
}
function updateNote(status){
	if(isNoteValidate.checkForm()){
		var html = html_encode($("#editorhtml").val());
		var md = html_encode($("#editormd").val());
		var url = "/HyperIT/note/updateNote";
		var parm = "userdetail_id="+thisUser.id+"&id="+noteId+"&title="+$("#title").val()+"&status="+status+"&content_html="+html+"&content_md="+md;
		$.post(url,parm,function(data){
			if(data.serviceResult){
				alert(data.resultInfo);
				history.go(-1);
			}else{
				if(data.resultInfo){
					messageTip(data.resultInfo);
				}else{
					$("#sensitive").text("文中出现了敏感词！       标题："+data.title+"     内容："+data.content_md+"      请修改后发布，您可以使用工具栏的搜索功能对文章内容中的敏感词进行搜索。");
				}
			}
		});
	}else{
		isNoteValidate.showErrors();
	}
}